# @Version : 1.0
# @Author  : wxz
# @File    : llms_clinet.py
# @Time    : 2025/6/18 22:21
# @Describe: 描述...
import asyncio
from typing import Optional

from autogen_core.models import UserMessage
from autogen_ext.models.openai import OpenAIChatCompletionClient

from config import app_settings
from config.app_logger import logger


class LLMsClient:

    _autogen_deepseek_chat_client: Optional[OpenAIChatCompletionClient] = None

    _autogen_qwen_chat_client: Optional[OpenAIChatCompletionClient] = None

    @classmethod
    def _create_openai_client(cls, model_config):
        """
        创建 AutoGen的模型客户端
        """
        try:
            return OpenAIChatCompletionClient(
                model=model_config.name,
                base_url=model_config.base_url,
                api_key=model_config.api_key,
                model_info={
                    "vision": model_config.model_info.vision,
                    "function_calling": model_config.model_info.function_calling,
                    "json_output": model_config.model_info.json_output,
                    "family": model_config.model_info.family,
                    "structured_output": model_config.model_info.structured_output,
                    "multiple_system_messages": model_config.model_info.multiple_system_messages,
                },
            )
        except Exception as e:
            logger.error(e, exc_info=True)

    @classmethod
    def autogen_deepseek_chat_client(cls):
        """
        AutoGen Deepseek 文本生成模型客户端
        """
        if cls._autogen_deepseek_chat_client is not None:
            return cls._autogen_deepseek_chat_client

        cls._autogen_deepseek_chat_client = cls._create_openai_client(app_settings.deepseek.chat_model)
        return cls._autogen_deepseek_chat_client

    @classmethod
    def autogen_qwen_chat_client(cls):
        """
        AutoGen Qwen 文本生成模型客户端
        """
        if cls._autogen_qwen_chat_client is not None:
            return cls._autogen_qwen_chat_client

        cls._autogen_qwen_chat_client = cls._create_openai_client(app_settings.qwen.chat_model)
        return cls._autogen_qwen_chat_client

async def main():

    client = LLMsClient.autogen_qwen_chat_client()

    result = await client.create(
        [UserMessage(content="你好？介绍一下你自己", source="user")]
    )
    print(result)

if __name__ == '__main__':
    asyncio.run(main())





